{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "html_str = \"\"\"\n",
    "<body>\n",
    "<div id=\"content\" class=\"ui container\">\n",
    "\n",
    "        <table id=\"contentbody\" class=\"ui striped  table\">\n",
    "            <tr class=\"1\">\n",
    "                <th>姓名</th>\n",
    "                <th>性别</th>\n",
    "                <th>邮箱</th>\n",
    "                <th>电话</th>\n",
    "            </tr>\n",
    "            <tr class=\"2\">\n",
    "                <td><a href=\"zhangwei\">张伟</a></td>\n",
    "                <td>男</td>\n",
    "                <td>zhangwei@haoren.com</td>\n",
    "                <td>12138-111</td>\n",
    "            </tr>\n",
    "            <tr class=\"3\">\n",
    "                <td><a href=\"yifei\">一菲</a></td>\n",
    "                <td>女</td>\n",
    "                <td>yifei@haoren.com</td>\n",
    "                <td>12138-112</td>\n",
    "            </tr>\n",
    "            <tr class=\"4\">\n",
    "                <td><a href=\"xiaoxian\">小贤</a></td>\n",
    "                <td>男</td>\n",
    "                <td>xiaoxian@haoren.com</td>\n",
    "                <td>12138-113</td>\n",
    "            </tr>\n",
    "            <tr class=\"5\">\n",
    "                <td><a href=\"meijia\">美嘉</a></td>\n",
    "                <td>女</td>\n",
    "                <td>meijia@haoren.com</td>\n",
    "                <td>12138-114</td>\n",
    "            </tr>\n",
    "            <tr class=\"6\">\n",
    "                <td><a href=\"xiaobu\">小布</a></td>\n",
    "                <td>男</td>\n",
    "                <td>xiaobu@hundan.com</td>\n",
    "                <td>12138-115</td>\n",
    "            </tr>\n",
    "\n",
    "        </table>\n",
    "</div>\n",
    "<div class=\"two div\">two div</div>\n",
    "</body>\n",
    "\"\"\"\n",
    "\n",
    "html = etree.HTML(html_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ui container', 'two div']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.xpath('.//div/@class')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ui container']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.xpath('.//div[@id=\"content\"]/@class')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['two div']\n",
      "['ui container', 'two div']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@class=\"two div\"]/@class'))\n",
    "print(html.xpath('.//div[contains(@class, \"o\")]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element table at 0x7fe0bd6cb500>]\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//table'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element tr at 0x7fe0bd6ce5f0>, <Element tr at 0x7fe0bd6ce640>, <Element tr at 0x7fe0bd6ce690>, <Element tr at 0x7fe0bd6ce6e0>, <Element tr at 0x7fe0bd6ce730>, <Element tr at 0x7fe0bd6ce780>]\n",
      "[<Element tr at 0x7fe0bd6ce5f0>, <Element tr at 0x7fe0bd6ce640>, <Element tr at 0x7fe0bd6ce690>, <Element tr at 0x7fe0bd6ce6e0>, <Element tr at 0x7fe0bd6ce730>, <Element tr at 0x7fe0bd6ce780>]\n",
      "------------------------------\n",
      "[]\n",
      "[<Element td at 0x7fe0bd6ce640>, <Element td at 0x7fe0bd6ce690>, <Element td at 0x7fe0bd6ce6e0>, <Element td at 0x7fe0bd6ce730>, <Element td at 0x7fe0bd6ce780>, <Element td at 0x7fe0bd6ce230>, <Element td at 0x7fe0bd6ce370>, <Element td at 0x7fe0bd6ce820>, <Element td at 0x7fe0bd6ce7d0>, <Element td at 0x7fe0bd6ce870>, <Element td at 0x7fe0bd6ce8c0>, <Element td at 0x7fe0bd6ce910>, <Element td at 0x7fe0bd6ce960>, <Element td at 0x7fe0bd6ce9b0>, <Element td at 0x7fe0bd6cea00>, <Element td at 0x7fe0bd6cea50>, <Element td at 0x7fe0bd6ceaa0>, <Element td at 0x7fe0bd6ceaf0>, <Element td at 0x7fe0bd6ceb40>, <Element td at 0x7fe0bd6ceb90>]\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//table/tr'))\n",
    "print(html.xpath('.//table//tr'))\n",
    "print('---' * 10)\n",
    "print(html.xpath('.//table/td'))\n",
    "print(html.xpath('.//table//td'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['男', '男', '男']\n",
      "['2', '4', '6']\n",
      "['张伟', '男', 'zhangwei@haoren.com', '12138-111', '小贤', '男', 'xiaoxian@haoren.com', '12138-113', '小布', '男', 'xiaobu@hundan.com', '12138-115']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//table//td[text()=\"男\"]/text()')) # 子孙级td，检索性别为男\n",
    "print(html.xpath('.//table//td[text()=\"男\"]/../@class')) # 子孙级td，检索性别为男\n",
    "print(html.xpath('.//table//td[text()=\"男\"]/../td//text()')) # 子孙级td，检索性别为男的tr标签，下的td标签，姓名/性别/邮箱/电话 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "好人: ['张伟', '一菲', '小贤', '美嘉']\n",
      "混蛋: ['小布']\n"
     ]
    }
   ],
   "source": [
    "print(\"好人:\", html.xpath('.//table//td[contains(text(), \"haoren\")]/../td/a/text()'))\n",
    "print(\"混蛋:\", html.xpath('.//table//td[contains(text(), \"hundan\")]/../td/a/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
